 //
//  HomeListCell.m
//  MPTLive
//
//  Created by HouGeng on 16/1/27.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "HomeListCell.h"
#import "TPImageView.h"
#import "MPTTool.h"

@interface HomeListCell()

@property (nonatomic, strong) UIImageView *imageVBig;

@property (nonatomic, strong) TPImageView *imageVHead;

@property (nonatomic, strong) UILabel *labName;

@property (nonatomic, strong) TPImageView *imageVideoState;

@property (nonatomic, strong) UILabel *LabContent;

@property (nonatomic, strong) TPLabel *LabNumber;

@property (nonatomic, strong) TPImageView *imageLuBo;

@property (nonatomic, strong) TPLabel *labLuBoTime;

/// 大V
@property (nonatomic, strong) TPImageView *imageBigV;

@end

@implementation HomeListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _imageVBig = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenW)];
        _imageVBig.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:_imageVBig];
        
        _imageVHead = [TPImageView getWithFrame:CGRectMake(15, screenW - 7.5, 40, 40)];
        _imageVHead.layer.borderWidth = 1;
        _imageVHead.layer.borderColor = [UIColor whiteColor].CGColor;
        [_imageVHead addTarget:self action:@selector(imageVHeadClicked)];
        _imageVHead.layer.cornerRadius = 20;
        [self.contentView addSubview:_imageVHead];
        
        /// 大V
        _imageBigV = [TPImageView getWithFrame:CGRectMake(44, CGRectGetMaxY(_imageVHead.frame) - 13, 13, 13) image:[UIImage imageNamed:@"bigV"]];
        _imageBigV.hidden = YES;
        [self.contentView addSubview:_imageBigV];
        
        _imageVideoState = [TPImageView getWithFrame:CGRectMake(screenW - 15 - 51, 15, 51, 22)];
        _imageVideoState.image =[UIImage imageNamed:@"livebiaoqian"];
        _imageVideoState.hidden = YES;
        [self.contentView addSubview:_imageVideoState];
        
        _imageLuBo = [TPImageView getWithFrame:CGRectMake(screenW - 15 - 106, 15, 106, 24)];
        _imageLuBo.image =[UIImage imageNamed:@"livelubo"];
        _imageLuBo.hidden = YES;
        [self.contentView addSubview:_imageLuBo];
        
        _labLuBoTime = [TPLabel getWithFrame:CGRectMake(106 / 2, 0, 106/ 2, 24) text:@"" textColor:[UIColor whiteColor] textFont:[UIFont systemFontOfSize:11]];
        _labLuBoTime.textAlignment = NSTextAlignmentCenter;
        [_imageLuBo addSubview:_labLuBoTime];
        
        
        CGFloat fltW = (screenW - 30 -7.5 - 40) / 2.0;
        _labName = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_imageVHead.frame) + 7.5, screenW+5, fltW, 40 - 7.5)];
        _labName.textColor = RGBAAllColor(0x282828, 1);
        _labName.font = [UIFont systemFontOfSize:14.0f];
        _labName.textAlignment = NSTextAlignmentLeft;
        _labName.text = @"";
        [self.contentView addSubview:_labName];
        
        
        _LabNumber = [[TPLabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_labName.frame), screenW + 5, fltW, 39 - 7.5)];
        _LabNumber.textColor = RGBAAllColor(0xF04433, 1);
        _LabNumber.font = [UIFont systemFontOfSize:14.0f];
        _LabNumber.textAlignment = NSTextAlignmentRight;
        _LabNumber.text = @"";
        [self.contentView addSubview:_LabNumber];
        
        
        _LabContent = [[UILabel alloc] initWithFrame:CGRectMake(15, screenW +CGRectGetHeight(_LabNumber.frame), screenW - 30, 68 - CGRectGetHeight(_LabNumber.frame))];
        _LabContent.textColor = RGBAAllColor(0x666666, 1);
        _LabContent.font = [UIFont systemFontOfSize:14.0f];
        _LabContent.textAlignment = NSTextAlignmentLeft;
        _LabContent.text = @"";
        [self.contentView addSubview:_LabContent];
        
        
        UIView *viewTiao = [[UIView alloc] initWithFrame:CGRectMake(0, screenW + 68, screenW, 10)];
        viewTiao.backgroundColor = RGBAColor(235, 235, 235, 1);
        [self.contentView addSubview:viewTiao];
        
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, screenW + 67.5, screenW, 0.5)];
        viewLine.backgroundColor = RGBAllColor(0xdddddd);
        [self.contentView addSubview:viewLine];
    }
    
    return self;
}

- (void)setDicData:(NSDictionary *)dicData
{
    if (_dicData != dicData)
    {
        _dicData = dicData;
        
        NSDictionary *dic = _dicData[@"channel"];
        
        /// 视频状态
        if ([dic[@"liveStatus"] integerValue] == 1)
        {
            _imageVideoState.hidden = NO;
            _imageLuBo.hidden = YES;
        }
        else
        {
            _imageVideoState.hidden = YES;
            _imageLuBo.hidden = NO;
            /// 时长
            _labLuBoTime.text = _dicData[@"channel"][@"ext"][@"finishTimeNice"];
        }
        
        /// 大图
        [_imageVBig sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",dic[@"pic"][@"base"],dic[@"pic"][@"m"]]] placeholderImage:[UIImage imageNamed:@"listQueSheng"]];
        
        /// 头像
        [_imageVHead setImageURL:dic[@"ext"][@"owner"][@"icon"]];
        
        /// 名称
        _labName.text = dic[@"ext"][@"owner"][@"nick"];
        
        /// 标题
        _LabContent.text = ZB_Str_Is_Valid(dic[@"ext"][@"t"])?dic[@"ext"][@"t"]:[NSString stringWithFormat:@"%@的直播", dic[@"ext"][@"owner"][@"nick"]];
        
        /// 是否显示大V
        _imageBigV.hidden = [dic[@"ext"][@"owner"][@"v"] integerValue]== 0;
        
        /// 视频状态
        if ([dic[@"liveStatus"] integerValue] == 1)
        {
            /// 观看数
            _LabNumber.text = [NSString stringWithFormat:@"%@ 人在看", [MPTTool getNumWith:[dic[@"stat"][@"ucnt"] integerValue]]];
            [_LabNumber setTextColor:RGBAllColor(0x666666) textFont:[UIFont systemFontOfSize:12] range:[_LabNumber.text rangeOfString:@"人在看"]];
        }
        else
        {
            /// 观看数
            _LabNumber.text = [NSString stringWithFormat:@"%@ 次观看", [MPTTool getNumWith:[dic[@"stat"][@"ucnt"] integerValue]]];
            [_LabNumber setTextColor:RGBAllColor(0x666666) textFont:[UIFont systemFontOfSize:12] range:[_LabNumber.text rangeOfString:@"次观看"]];
        }
    }
}

- (void)imageVHeadClicked
{
    if (self.block)
    {
        self.block(_indexpath);
    }
}

@end
